import { request } from '@/api/request';
import { getOrgEntType } from '@/components/EntTypeSelect/utils';
import { getStudyObjectString } from '@/components/StudyObjectSelect/utils';
import { getTradeGroupString, tradeGroupResult } from '@/components/Trade/utils';
import Shelf from '../Shelf';

/**
 * @desc 未上架的学习计划模版接口的返回值
 * @address /safe/api/industry/plan/list
 */
interface CoursePlanRes {
  planId: string;
  planName: string;
  learnMode: string;
  cycleType: string;
  learnType?: string;
  learnObj: string;
  region?: string;
  classId?: string;
  learnStartTime?: string;
  learnEndTime?: string;
  ctPerson?: string;
  state: string;
  faceImg?: string;
  industryCode?: string;
  industryName?: string;
  isDel: string;
  ctDate: string;
  upDate: string;
  cycleDay?: string;
  coursewares?: any[];
  shelvesCopy: number;
  isShelves: number;
  entTypeCode?: string;
  trade?: string;
  isCharge?: number;
  copyId?: string;
  regionName?: string;
  tradeName?: string;
  showTrade?: string;
}

/**
 * @desc 上架的学习计划模版接口的返回值
 * @address /safe/api/shelves/planList
 */
interface GovernmentAssociation {
  guid: string;
  type?: unknown;
  region: string;
  entTypeCode: string;
  trade: string;
  isCharge?: unknown;
  regionName: string;
  name: string;
  classId?: unknown;
  learnObj: string;
  relationNum?: unknown;
  creatTime: string;
  learnMode: string;
  cycleType: string;
  learnType?: unknown;
  learnStartTime?: unknown;
  learnEndTime?: unknown;
  cycleDay?: unknown;
  faceImg: string;
  tradeName: string;
  updateTime?: unknown;
  showTrade?: string;
}

class StudyPlan {
  static shelf = new Shelf(2);
  name: string;
  guid: string;
  origin: CoursePlanRes | GovernmentAssociation;
  learnObjText?: string;
  entTypeCodeText?: string;
  regionName?: string;
  showTradeArry?: tradeGroupResult[];
  tradeName?: string;
  showTrade?: string;
  isCharge?: number;
  constructor(id: string, name: string, origin: CoursePlanRes | GovernmentAssociation) {
    this.name = name;
    this.guid = id;
    this.origin = origin;
    this.learnObjText = getStudyObjectString(origin.learnObj);
    this.entTypeCodeText = getOrgEntType(origin.entTypeCode);
    this.showTradeArry = getTradeGroupString(origin.showTrade);
    this.showTrade = origin.showTrade ? origin.showTrade : '';
    // this.showTradeArry = origin.showTrade ? JSON.parse(origin.showTrade) : [];
    this.regionName = this.origin.regionName;
    this.tradeName = this.origin.tradeName;
    this.isCharge = Number(this.origin.isCharge);
  }

  get cover() {
    return this.origin.faceImg ?? undefined;
  }
  get cycleType() {
    return this.origin.cycleType;
  }

  get cycleDay() {
    return this.origin.cycleDay ?? undefined;
  }

  get learnObj() {
    return this.origin.learnObj;
  }

  get learnMode() {
    return this.origin.learnMode ?? undefined;
  }

  get createTime() {
    if ('creatTime' in this.origin) {
      return this.origin.creatTime;
    } else {
      return this.origin.ctDate;
    }
  }

  /**
   * @desc 区域
   */
  get area() {
    return this.origin.regionName;
  }

  /**
   * @desc 行业
   */
  get trade() {
    return this.origin.tradeName;
  }

  static async unShelfList(pageNum: number, pageSize: number, params: { learnObj?: string | number }) {
    const { data } = await request<{ total: number; list: CoursePlanRes[] }>({
      url: '/safe/api/industry/plan/list',
      params: {
        pageNum,
        pageSize,
        ...params,
      },
    });
    const newList = data.list.map((v) => {
      return new StudyPlan(v.planId, v.planName, v);
    });
    return {
      list: newList,
      total: data.total,
    };
  }
  static async onShelfList(pageNum: number, pageSize: number, params: { learnObj?: string | number }) {
    const { data } = await request<{ total: number; list: GovernmentAssociation[] }>({
      url: '/safe/api/shelves/planList',
      data: {
        pageNum,
        pageSize,
        ...params,
      },
    });
    return {
      list: data.list.map((v) => {
        return new StudyPlan(v.guid, v.name, v);
      }),
      total: data.total,
    };
  }

  //学习计划新增
  static async createOrUpdatePlan({ planId, ...params }: { planId?: string } & Record<string, unknown>) {
    const { data } = await request({
      url: `/safe/api/industry/plan/${planId ? 'update' : 'create'}`,
      data: {
        ...params,
        planId,
      },
    });
    return data;
  }
  /**
   * @desc 创建或者更新一个学习计划模版
   */
  static async createOrUpdate({ planId, ...params }: { planId?: string } & Record<string, unknown>) {
    await request({
      url: `/safe/api/industry/plan/${planId ? 'update' : 'create'}`,
      data: {
        ...params,
        planId,
      },
    });
    return true;
  }

  static async delete(planId: string) {
    await request({
      url: '/safe/api/industry/plan/delete',
      params: {
        planId,
      },
    });
  }
}

export default StudyPlan;
